package com.ctrl.riveradministrator.mvp.presenter;

import com.ctrl.riveradministrator.base.BaseObserver;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.http.UploadRequestBody;
import com.ctrl.riveradministrator.RaApplication;
import com.ctrl.riveradministrator.mvp.view.activity.illegal_report.IllegalReportView;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class IllegalReportPresenter extends BasePresenter<IllegalReportView> {

    public IllegalReportPresenter(IllegalReportView baseView) {
        super(baseView);
    }


   /* public void report(String type, String content) {
        Map<String, String> map = new HashMap<>();
        map.put("userid", Objects.requireNonNull(RaApplication.getUserId()));
        map.put("type", type);
        map.put("content", content);
        attachView(apiService.illegalReport(map), new BaseObserver<Report>(getBaseView()) {
            @Override
            public void onSuccess(Report model) {
                Log.d("onSuccess", ":");
            }

            @Override
            public void onSuccess(List<Report> model) {

            }

            @Override
            public void onFailed(String msg) {
                Log.d("onFailed", ":" + msg);
            }

        });
    }*/

    public void report(List<String> images, String type, String content) {

        for (int i = 0; i < images.size(); i++) {
            File file = new File(images.get(i));
            RequestBody requestFile =
                    RequestBody.create(MediaType.parse("multipart/form-data"), file);
            UploadRequestBody fileRequestBody = new UploadRequestBody(requestFile, (currentLength, contentLength) -> {
                //获取上传的比例
                getBaseView().uploading(currentLength + "/" + contentLength);
            });
            //files是与服务器对应的key
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("files", file.getName(), fileRequestBody);
            attachView(apiService.illegalReport(
                    RaApplication.getUserId(),
                    type,
                    content,
                    body), new BaseObserver(getBaseView()) {
                @Override
                public void onSuccess(Object model) {
                    getBaseView().showMessage();
                }

                @Override
                public void onSuccess(List model) {
                    getBaseView().showMessage();
                }

                @Override
                public void onFailed(String msg) {

                }
            });
        }

    }
}
